package server;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.stereotype.Component;
import server.reids.RedisUtil;

@Component
public class MyRedisClientDetailsService implements ClientDetailsService {


    private RedisTemplate redisTemplate;


    public MyRedisClientDetailsService(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {

        ClientDetails details = (ClientDetails) redisTemplate.opsForValue().get(RedisUtil.createKey("t_client", clientId));
        if (details == null) {
            throw new NoSuchClientException("No client with requested id: " + clientId);
        }
        return details;
    }

}
